package com.example.elmmerchants.Ringtone_Setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Ringtone_Setting_Page extends AppCompatActivity {

    RelativeLayout listening_ringtone;
    TextView how_many_book_almost_time, how_many_book_has_ride_get_order, how_many_new_order, how_many_auto_get_order, how_many_reminder_order, how_many_order_cancel, how_many_user_apply_refund, how_many_abnormal_distribution;
    String email;
    int book_almost_time, book_has_ride_get_order, new_order, auto_get_order, reminder_order, order_cancel, user_apply_refund, abnormal_distribution, how_many_volume;
    Button use_my_method;
    boolean is_open_vibrate = false;
    Switch is_open_vibrate_switch;
    SeekBar volume;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringtone_setting_page);
        email = getIntent().getStringExtra("email");
        book_almost_time = getIntent().getIntExtra("book_almost_time", 0);
        book_has_ride_get_order = getIntent().getIntExtra("book_has_ride_get_order", 0);
        new_order = getIntent().getIntExtra("new_order", 0);
        auto_get_order = getIntent().getIntExtra("auto_get_order", 0);
        reminder_order = getIntent().getIntExtra("reminder_order", 0);
        order_cancel = getIntent().getIntExtra("order_cancel", 0);
        user_apply_refund = getIntent().getIntExtra("user_apply_refund", 0);
        abnormal_distribution = getIntent().getIntExtra("abnormal_distribution", 0);
        how_many_volume = getIntent().getIntExtra("how_many_volume", 0);
        is_open_vibrate = getIntent().getBooleanExtra("is_open_vibrate", false);

        listening_ringtone = findViewById(R.id.listening_ringtone);
        how_many_book_almost_time = findViewById(R.id.how_many_book_almost_time);
        how_many_book_has_ride_get_order = findViewById(R.id.how_many_book_has_ride_get_order);
        how_many_new_order = findViewById(R.id.how_many_new_order);
        how_many_auto_get_order = findViewById(R.id.how_many_auto_get_order);
        how_many_reminder_order = findViewById(R.id.how_many_reminder_order);
        how_many_order_cancel = findViewById(R.id.how_many_order_cancel);
        how_many_user_apply_refund = findViewById(R.id.how_many_user_apply_refund);
        how_many_abnormal_distribution = findViewById(R.id.how_many_abnormal_distribution);
        use_my_method = findViewById(R.id.use_my_method);
        is_open_vibrate_switch = findViewById(R.id.is_open_vibrate);
        volume = findViewById(R.id.volume);

        use_my_method.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "update `merchant_ringtone_setting` set book_almost_time = ?, book_has_ride_get_order = ?, new_order = ?, auto_order = ?, reminder_order = ?, order_cancel = ?, user_apply_refund = ?, abnormal_distribution = ? where email = ?";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, 3);
                        preparedStatement.setInt(2, 3);
                        preparedStatement.setInt(3, 3);
                        preparedStatement.setInt(4, 1);
                        preparedStatement.setInt(5, 1);
                        preparedStatement.setInt(6, 3);
                        preparedStatement.setInt(7, 3);
                        preparedStatement.setInt(8, 3);
                        preparedStatement.setString(9, email);
                        preparedStatement.executeUpdate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

                runOnUiThread(() -> {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    Intent i = new Intent(Ringtone_Setting_Page.this, Ringtone_Setting_Page.class);
                    finish();
                    i.putExtra("email", email);
                    overridePendingTransition(0, 0);
                    startActivity(i);
                    overridePendingTransition(0, 0);
                });
            }
        });

        how_many_book_almost_time.setText(String.valueOf(book_almost_time));
        how_many_book_has_ride_get_order.setText(String.valueOf(book_has_ride_get_order));
        how_many_new_order.setText(String.valueOf(new_order));
        how_many_auto_get_order.setText(String.valueOf(auto_get_order));
        how_many_reminder_order.setText(String.valueOf(reminder_order));
        how_many_order_cancel.setText(String.valueOf(order_cancel));
        how_many_user_apply_refund.setText(String.valueOf(user_apply_refund));
        how_many_abnormal_distribution.setText(String.valueOf(abnormal_distribution));
        volume.setProgress(how_many_volume);

        listening_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Ringtone_Setting_Page.this, Ringtone_Try_listen.class);
                intent.putExtra("is_open_vibrate", is_open_vibrate);
                intent.putExtra("how_many_volume", how_many_volume);
                startActivity(intent);
            }
        });

        volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                how_many_volume = i;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "update `merchant_ringtone_setting` set volume = ? where email = ?";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, how_many_volume);
                        preparedStatement.setString(2, email);
                        preparedStatement.executeUpdate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });

        if (is_open_vibrate) {
            is_open_vibrate_switch.setChecked(true);
        }
        else {
            is_open_vibrate_switch.setChecked(false);
        }

        is_open_vibrate_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_vibrate_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_ringtone_setting` set is_open_vibrate = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_vibrate_switch.setChecked(true);
                            is_open_vibrate = true;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_ringtone_setting` set is_open_vibrate = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_vibrate_switch.setChecked(false);
                            is_open_vibrate = false;
                        });
                    });
                }
            }
        });
    }
}